package ${t.packageName}.controller;

import com.oleasy.platform.api.common.restapi.Result;
import com.oleasy.platform.api.common.controller.base.BaseController;
import com.oleasy.platform.api.common.restapi.ResultCode;
import com.oleasy.platform.api.common.utils.BeanUtils;
import ${t.packageName}.model.entity.${t.entityName};
import ${t.packageName}.model.request.${t.entityName}Query;
import ${t.packageName}.model.request.${t.entityName}Request;
import ${t.packageName}.service.${t.entityName}Service;
import org.apache.commons.collections4.CollectionUtils;
import com.oleasy.platform.api.common.utils.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.transaction.Transactional;
import java.util.List;

@RestController
@Api(value = "${t.name}", tags = "${t.name}")
@RequestMapping("/api/${t.lowEntityName}")
public class ${t.entityName}Controller extends BaseController {
    @Resource
    private ${t.entityName}Service ${t.lowEntityName}Service;

    @ApiOperation(value = "保存", notes = "")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "X-OLT-TOKEN", dataType = "string", paramType = "header")
    })
    @PostMapping("/")
    public Result<${t.entityName}> save(@RequestBody ${t.entityName}Request ${t.lowEntityName}Request) {
        return Result.ok(${t.lowEntityName}Service.save(${t.lowEntityName}Request));
    }

    @ApiOperation(value = "列表")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "X-OLT-TOKEN", dataType = "string", paramType = "header")
    })
    @GetMapping("/")
    public Result<Page<${t.entityName}>> list(${t.entityName}Query ${t.lowEntityName}Query) {
        return Result.ok(${t.lowEntityName}Service.find(${t.lowEntityName}Query));
    }

    @ApiOperation(value = "单条信息")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "X-OLT-TOKEN", dataType = "string", paramType = "header")
    })
    @GetMapping("/{id}")
    public Result<${t.entityName}> single(@PathVariable String id) {
    ${t.entityName} ${t.lowEntityName} =
        BeanUtils.copyProperties(${t.lowEntityName}Service.findById(id).get(), ${t.entityName}. class);
        return Result.ok(${t.lowEntityName});
    }

    @ApiOperation(value = "删除", notes = "提交要删除的ID数组，批量删除")
    @ApiImplicitParams(value = {@ApiImplicitParam(name = "X-OLT-TOKEN", dataType = "string", paramType = "header")})
    @DeleteMapping("/")
    @Transactional
    public Result<String> delete(@RequestBody List<String> idList) {
        // 数据合法性、删除权限
        if (CollectionUtils.isEmpty(idList)) {
            return Result.error(ResultCode.ID_IS_NULL);
        }
        if (idList.stream().anyMatch(StringUtils::isBlank)) {
            return Result.error(ResultCode.ID_IS_NULL);
        }
        ${t.lowEntityName}Service.delete(idList);

        return Result.ok();
    }

}